package com.gzy.mylog.security;

import com.gzy.mylog.server.AuthServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.annotation.web.configurers.ExpressionUrlAuthorizationConfigurer;

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true, jsr250Enabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    public static final byte[] KEY = "123456".getBytes();

    @Autowired
    AuthServer authServer;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable();
        // 配置 接口的验证
        http.authorizeRequests()
                .antMatchers(HttpMethod.OPTIONS).permitAll()
                .antMatchers("/login").permitAll()
                .antMatchers("/auth/logout").permitAll()
                .antMatchers("/signup").permitAll()
                .antMatchers("/upload").permitAll()
                .antMatchers("/download/*").permitAll()
//                .antMatchers("/userinfo").hasRole("").permitAll()
//                .antMatchers("/login2").permitAll()
                .anyRequest()
                .authenticated();

        http.addFilter(new JWTFilter(authenticationManager(),authServer));
    }
}
